package com.svs.common.utils;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import org.apache.commons.codec.binary.Base64;

public class CreateSecrteKey {
	
	private static  String PUBLIC_KEY = "";

	private static  String PRIVATE_KEY = "";
    
    /** 
	 * 随机生成密钥对 
	 * @throws NoSuchAlgorithmException 
	 */  
	public static void genKeyPair() throws NoSuchAlgorithmException {  
		// KeyPairGenerator类用于生成公钥和私钥对，基于RSA算法生成对象  
		KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");  
		// 初始化密钥对生成器，密钥大小为96-1024位  
		keyPairGen.initialize(1024,new SecureRandom());  
		// 生成一个密钥对，保存在keyPair中  
		KeyPair keyPair = keyPairGen.generateKeyPair();  
		RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();   // 得到私钥  
		RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();  // 得到公钥 
		
		PUBLIC_KEY = new String(Base64.encodeBase64(publicKey.getEncoded()));		
		// 得到私钥字符串  
		PRIVATE_KEY = new String(Base64.encodeBase64((privateKey.getEncoded())));  
	} 

    public static void main(String[] args) {
        try {
        	genKeyPair();
        	System.out.println("随机生成的公钥为:" + PUBLIC_KEY);
    		System.out.println("随机生成的私钥为:" + PRIVATE_KEY);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
